package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.AdvIncome;
import com.ruoyi.mapper.AdvIncomeMapper;
import com.ruoyi.mapper.AdvMembersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.AdvIncomedetailMapper;
import com.ruoyi.domain.AdvIncomedetail;
import com.ruoyi.service.IAdvIncomedetailService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class AdvIncomedetailServiceImpl implements IAdvIncomedetailService
{
    @Autowired
    private AdvIncomedetailMapper advIncomedetailMapper;
    @Autowired
    private AdvIncomeMapper advIncomeMapper;
    @Autowired
    private AdvMembersMapper advMembersMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param advIncomedetailId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AdvIncomedetail selectAdvIncomedetailByAdvIncomedetailId(Long advIncomedetailId)
    {
        return advIncomedetailMapper.selectAdvIncomedetailByAdvIncomedetailId(advIncomedetailId);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param advIncomedetail 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AdvIncomedetail> selectAdvIncomedetailList(AdvIncomedetail advIncomedetail)
    {
        return advIncomedetailMapper.selectAdvIncomedetailList(advIncomedetail);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param advIncomedetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAdvIncomedetail(AdvIncomedetail advIncomedetail)
    {
        return advIncomedetailMapper.insertAdvIncomedetail(advIncomedetail);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param advIncomedetail 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAdvIncomedetail(AdvIncomedetail advIncomedetail)
    {
        return advIncomedetailMapper.updateAdvIncomedetail(advIncomedetail);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param advIncomedetailIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdvIncomedetailByAdvIncomedetailIds(Long[] advIncomedetailIds)
    {
        try {
            return advIncomedetailMapper.deleteAdvIncomedetailByAdvIncomedetailIds(advIncomedetailIds);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }

    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param advIncomedetailId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAdvIncomedetailByAdvIncomedetailId(Long advIncomedetailId)
    {
        try {
            return advIncomedetailMapper.deleteAdvIncomedetailByAdvIncomedetailId(advIncomedetailId);
        }catch (DataIntegrityViolationException e){
            throw new RuntimeException("请先删除关联数据");
        }

    }

    @Override
    public List<AdvIncomedetail> getIncomedetail(String advIncomeId) {
        // 1. 根据传递过来的 advIncomeId 查询到对应的AdvIncomedetail [一级收益清单]
        List<AdvIncomedetail> list = advIncomedetailMapper.getIncomedetail(advIncomeId);
        // 2. 根据 advIncomeId 查询到对应的收益信息
        AdvIncome advIncome = advIncomeMapper.selectAdvIncomeByAdvIncomeId(Long.valueOf(advIncomeId));
        // 3. 根据查询到的 advIncome 中的 membersId 获取到子级会员 id集合
        List<Long> nextIds = advMembersMapper.getNextIds(Long.valueOf(advIncome.getAdvMembersId()));
        // 4. 根据 advIncome 中的 时间信息查询到同一时间结算的收益订单
        AdvIncome params = new AdvIncome();
        params.setCalDate(advIncome.getCalDate());
        params.setCalYear(advIncome.getCalYear());
        params.setCalMonth(advIncome.getCalMonth());
        List<AdvIncome> sameTimeIncomes = advIncomeMapper.selectAdvIncomeList(params); // 同一时间的收益订单
        // 5. 遍历 同一时间的收益订单, 判断收益订单的 membersId 是否是所选收益订单中 membersId 的下级
        for (AdvIncome sameTimeIncome : sameTimeIncomes) {
            if(nextIds.contains(Long.valueOf(sameTimeIncome.getAdvMembersId()))) {
                // 5.1 如果是 所选收益订单中 membersId 的下级, 查询出该收益订单的明细, 添加到结果list中
                List<AdvIncomedetail> nextList = advIncomedetailMapper
                        .getIncomedetail(String.valueOf(sameTimeIncome.getAdvIncomeId()));
                list.addAll(nextList);
            }
        }
        return list;
    }
}
